package com.yusi.controller;


import com.yusi.entity.Pathdis;
import com.yusi.entity.dto.RespBean;
import com.yusi.entity.dto.RespPageBean;
import com.yusi.service.IPathdisService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yusi
 * @since 2022-01-10
 */
@RestController
@RequestMapping("/air/path")
public class PathdisController {
    @Autowired
    private IPathdisService iPathdisService;

    @ApiOperation("查看航线信息分页")
    @GetMapping("/airlines")
    public RespPageBean getAirlines(
                @RequestParam(defaultValue = "1")Integer currentpage,
                @RequestParam(defaultValue = "10")Integer size,
                String airport1,
                String airport2,
                LocalDate[] beginDateScope){
        return iPathdisService.getAirlines(currentpage,size,airport1,airport2,beginDateScope);
    }

    @ApiOperation("新增航线")
    @PostMapping("/airline")
    @Transactional
    public RespBean saveAirLine(@RequestBody Pathdis pathdis){
        System.out.println(pathdis);
        if (iPathdisService.save(pathdis)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }
    @ApiOperation("删除航线")
    @DeleteMapping("/airline/{id}")
    @Transactional
    public RespBean delAirLine(@PathVariable("id") Integer id){
        if (iPathdisService.removeById(id)){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }
    @ApiOperation("删除航线")
    @DeleteMapping("/airlines/")
    @Transactional
    public RespBean delAirLines(Integer[] ids){
        if (ids.length==0){
            return RespBean.error("数据不能为null");
        }
        if (iPathdisService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation("修改航线")
    @PutMapping("/airline")
    @Transactional
    public RespBean updAirLine(@RequestBody Pathdis pathdis){
        if (iPathdisService.updateById(pathdis)){
            return RespBean.success("修改成功！");
        }
        return RespBean.error("修改失败！");
    }


}
